home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_corktile.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  4.4 KB  |  122 lines

  1. /* Renamed to PQcorktile.sl for RMR -- talrmr@SpamSucks_pacbell.net */
  2.  
  3. /* corktile.sl - a surface shader imitating cork tiling
  4.  *
  5.  * Author: Peter Quint
  6.  * Last Update: 16 October 2000
  7.  *
  8.  * parameters
  9.  *
  10.  * Ka, Kd, Ks, roughness, specularcolor - take their usual meanings
  11.  * Ksgap - the specular component for tile gaps : by default 0.3 * Ks
  12.  * txtscale - the amount of detail / magnification of the image, this also determines
  13.  *   (if tiles=1) the number of tiles fitted into the texture as s and t vary from 0 to 1
  14.  * tiles - the number of tiles in a unit square of texture before txtscale is applied
  15.  *     (this means the number of tiles actually seen is the square of txtscale * tiles)
  16.  * gap - the size of the gap or groove between tiles, measured as a proportion of the tile,
  17.  *     thus a gap of .05 will mean that 2.5% of the tile around each edge will be taken up with
  18.  *   the groove (2.5% either side = 5% in total = 0.05)
  19.  * bumpheight - the total height for bump mapped bumps - irregularities on the surface plus
  20.  *   the displacement of the tile itself out of the gap
  21.  * tileheight - the proportion of the bumpheight that is taken up with the rise of the tile from
  22.  *      the gap between the tiles, the remainder of bumpheight is taken up with the bumps on the 
  23.  *   surface of the tile. So if tileheight = 1 there are no bumps on the tile surface, if
  24.  *   tileheight = 0 there are bumps on the tile surface of height tileheight, but no difference
  25.  *   in height between the gap and the tile itself
  26.  *
  27.  * The shader makes an attempt at being self antaliasing
  28.  *
  29.  */
  30.  
  31. #include "k3d_noises.h" /* the standard BMRT include file */
  32. #include "k3d_tile.h"
  33. #define fnoise(p,width) (noise(p) * (1-smoothstep (0.2,0.75,width)))
  34.  
  35. /* Bias function - Perlin and Hoffert 1989 */
  36.  
  37. float
  38. bias (float b, x)
  39. {
  40.     return pow(x, log(b)/log(0.5));
  41. }
  42.  
  43. color cork(        point PP;
  44.                             float txtscale, filtwidth)
  45.     {
  46.         /*     the pattern is built up of the sum of two offset noise functions, perturbed slightly
  47.         and used to select a color from a spline, combined with an overlay of the dark 
  48.         markings that characterise cork
  49.         */
  50.         color Ct;
  51.         point P2 = transform("shader",PP);
  52.         point PT = P2 * txtscale;
  53.         float fw = filtwidth * txtscale;
  54.         float g;
  55.         float f = fnoise(PT, fw) + fnoise((P2 + 0.553) * txtscale, fw); 
  56.         f = f * fnoise(PT * .854, fw);
  57.         f = f + (fnoise(PT * 5.342, fw) - .5) * .5;  
  58.         f = clamp(f, 0, 1);
  59.         g = fBm((P2 + 8.654) * txtscale, fw, 4, 2, .5);
  60.         Ct = color spline(     f,
  61.                         color( 0, 0, 0),
  62.                         color( .25, 0, 0),
  63.                         color( .31, .2, 0),
  64.                         color( .486, .165, .059),
  65.                         color( .47, .18, .09),
  66.                         color( .64, .24, .07),
  67.                         color( .749, .416, .208)
  68.                         ); 
  69.         /* Overlay the dark markings */
  70.         if (g > .2)
  71.             {
  72.                 g = bias(0.8,(g - .2) / .8);
  73.                 Ct = Ct * (1 - g * .65);
  74.             }
  75.         return Ct;
  76.     }
  77.  
  78. surface
  79. k3d_corktile(     float    Ka = 1;
  80.               float    Kd = .75;
  81.                  float    Ks = .3;
  82.                         float Ksgap = -1;
  83.                  float    roughness = .05;
  84.                          float    txtscale = 1;
  85.                         float tiles = 1;
  86.                         float gap = .05;
  87.                         color specularcolor = 1;
  88.                         float bumpheight = 0.3;
  89.                         float tileheight = .8;
  90.                         float bumpfreq = 0.8;)
  91.  
  92. {
  93.     uniform float Ksg = Ksgap > 0 ? Ksgap : Ks * .3; 
  94.     uniform float tilewidth = 1 / (tiles * txtscale); 
  95.     float disp, adjust;
  96.     point PP = transform("shader",P);
  97.     float filtwidth = filterwidthp(PP),
  98.             swidth = abs(Du(s)*du) + abs(Dv(s)*dv),
  99.             twidth = abs(Du(t)*du) + abs(Dv(t)*dv),
  100.             stfiltwidth = max(swidth, twidth) / tilewidth,
  101.             sw, tw, so, to;
  102.     color Cg, Ct;
  103.     normal Nf;
  104.     float ingap = tile(s, t, tilewidth, tilewidth, swidth, twidth, gap, gap, sw, tw, so, to);
  105.     
  106.     Ct = cork( P, txtscale, filtwidth);    
  107.     /* Calculate color of gap between tiles */
  108.     Cg = color( .20, 0, 0) + filteredsnoise(PP * txtscale, filtwidth * txtscale) * .2;
  109.     so = so > .5 ? 1 - so : so;
  110.     to = to > .5 ? 1 - to : to; 
  111.     adjust = (1 - min(smoothstep(gap * .3, gap * .7, so), smoothstep(gap * .3, gap * .7, to))) * 
  112.         (1  - smoothstep(.2 * gap, gap * .8, stfiltwidth));  
  113.     disp = (tileheight + fnoise(PP * txtscale * bumpfreq + 3, filtwidth * txtscale) * (1 - tileheight)) 
  114.         * bumpheight * (1 - adjust);
  115.     Nf = faceforward(calculatenormal(P + disp *    normalize(N)), I);
  116.     Nf = normalize(Nf);
  117.     /* printf("Nf = %p\n", Nf);     */
  118.     Oi = Os;
  119.     Ci = Os * (mix( Ct, Cg, ingap) * (Ka*ambient() + Kd*diffuse(Nf)) +
  120.         specularcolor * Ks*specular(Nf,-normalize(I),roughness)); 
  121.     }
  122.